package com.woniu.shops.controller;

import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTUtil;
import com.woniu.common.constans.Constants;
import com.woniu.common.pojo.Shops;
import com.woniu.common.pojo.User;
import com.woniu.common.utils.TokenUtil;
import com.woniu.common.vo.*;
import com.woniu.shops.service.ShopsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/shops")
@CrossOrigin
public class ShopsController {
    @Resource
    private ShopsService shopsService;

    //    登入
    @PostMapping("/login")
    public AjaxResult logins(@RequestBody Shopss shopss, HttpServletResponse response) {
        ShopsDto shopsDto = shopsService.logins(shopss);
        return AjaxResult.success("登录成功", shopsDto.getToken());
    }

    //    注册
    @RequestMapping("/add")
    public AjaxResult add(@RequestBody Shops shops) {
        shopsService.add(shops);
        return AjaxResult.success("添加成功!");
    }

    //    查名字进行重名判断
    @PostMapping("/selectShopsName")
    public AjaxResult selectUserName(@RequestBody Shops shops) {
        int i = shopsService.selectShopsName(shops.getSname());
        if (i == 1) {
            return AjaxResult.success("该名字已存在，请重新输入。");
        }
        return AjaxResult.error("商家名称可用");
    }

    @RequestMapping("/selectOne")
    public AjaxResult selectOne(@RequestBody Shops shop) {
        Shops shops = shopsService.selectShop(shop);
        return AjaxResult.success("查询成功!", shops);
    }
    @RequestMapping("/updateShop")
    public AjaxResult updateShop(@RequestBody Shops shop) {
        shopsService.updateShop(shop);
        return AjaxResult.success("修改成功,请重新登录!");
    }
    @RequestMapping("/getTook")
    public AjaxResult getTook(HttpServletRequest request) {
        Shops obj = TokenUtil.getObj(request, Shops.class);
        if(obj != null && !obj.equals("")){
            return AjaxResult.success("修改成功,请重新登录!",obj);
        }
        return AjaxResult.error("没有登陆请登录");
    }



}
